package com.futvan.z.shop.order;
import com.futvan.z.erp.customer_notice.crm_customer_notice;
import com.futvan.z.framework.common.bean.Code;
import com.futvan.z.framework.common.bean.Result;
import com.futvan.z.framework.core.z;
import com.futvan.z.framework.util.DateUtil;
import com.futvan.z.framework.util.EasemobUtil;
import com.futvan.z.framework.util.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import com.futvan.z.framework.core.SuperAction;
import com.futvan.z.shop.order.OrderService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

@Controller
public class OrderAction extends SuperAction{
	@Autowired
	private OrderService orderService;

	@RequestMapping(value="/OrderSendGoods")
	public @ResponseBody
	Result Service(@RequestParam HashMap<String, String> bean) throws Exception {
		Result result = new Result();

		String zids = bean.get("zids");
			if (z.isNotNull(zids)){
				List<s_order> list = sqlSession.selectList("s_order_select_sql","select * from s_order where zid in ("+ StringUtil.ListToSqlInArray(zids)+")");
				boolean isok = true;
				String info = "";
				for (s_order o : list) {
					if(!"10".equals(o.getOrder_status()) && !"15".equals(o.getOrder_status())){
						isok = false;
						info = "订单状态必须为已付款";
						break;
					}
					if(z.isNull(o.getCourier_company())){
						isok = false;
						info = "快递公司不可为空";
						break;
					}
					if(z.isNull(o.getCourier_number())){
						isok = false;
						info = "快递单号不可为空";
						break;
					}
				}

				if(isok){
					//开始执行
					int n = sqlSession.update("update","update s_order set order_status = 20 where zid in ("+StringUtil.ListToSqlInArray(zids)+")");
					if(n!=list.size()){
						z.Exception("订单执行发货出错");
					}else{

						//发送订单发货通知
						SendCutomerNotice(zids);

						result.setSuccess();
					}
				}else{
					result.setError(info);
				}
			}else{
				result.setError("zids is null");
			}
		return result;
	}

	/**
	 * 发送，发货通知
	 * @param zids
	 */
	private void SendCutomerNotice(String zids) {
		if (z.isNotNull(zids)){
			String[] idarray = zids.split(",");
			for (String zid : idarray) {
				s_order o = sqlSession.selectOne("s_order_select_zid",zid);
				if(z.isNotNull(o)){
					if(o.getCreate_order_customer().equals(o.getBuyer_id())){
						//下单人，和购买人相同
						String title = "您的订单["+o.getZid()+"]已发货";
						StringBuffer info = new StringBuffer();
						info.append("<p>您的订单【"+o.getZid()+"】已发货,请关注物流信息。</p>");
						info.append("<hr>");
						info.append("<p>物流单号："+o.getCourier_number()+"</p>");
						info.append("<hr>");
						info.append("<p>收货地址："+o.getCustom_eaddress_complete()+"</p>");
						info.append("<p></p>");
						info.append("<p></p>");
						CreateCustomerNotice(o.getCreate_order_customer(),"2",title,info.toString());
					}else{
						//下单人与购买人不同
						String title = "您的订单["+o.getZid()+"]已发货";
						StringBuffer info = new StringBuffer();
						info.append("<p>您的订单【"+o.getZid()+"】已发货,请关注物流信息。</p>");
						info.append("<hr>");
						info.append("<p>物流单号："+o.getCourier_number()+"</p>");
						info.append("<hr>");
						info.append("<p>收货地址："+o.getCustom_eaddress_complete()+"</p>");
						info.append("<p></p>");
						info.append("<p></p>");
						//下单人
						CreateCustomerNotice(o.getCreate_order_customer(),"2",title,info.toString());
						//买家
						CreateCustomerNotice(o.getBuyer_id(),"2",title,info.toString());
					}
				}
			}

		}
	}

	/**
	 * 创建通知
	 * @param customerid
	 * @param title
	 * @param info
	 */
	private void CreateCustomerNotice(String customerid,String notice_type, String title, String info) {
		crm_customer_notice c = new crm_customer_notice();
		String zid = z.newNumber();
		c.setZid(zid);
		c.setNumber(zid);
		c.setCreate_time(DateUtil.getDateTime());
		c.setCustomid(customerid);
		c.setNotice_type(notice_type);
		c.setIs_read("0");
		c.setTitle(title);
		c.setInfo(info);
		sqlSession.insert("crm_customer_notice_insert",c);

		//通知APP
//		Result result = EasemobUtil.Send(customerid,z.codeValue.get("notice_type_"+notice_type),title);
//		if(result.ifSuccess()){
//			System.out.println("通知APP|"+customerid+"|"+result.getMsg());
//		}else{
//			System.out.println("通知APP|"+customerid+"|"+result.getMsg()+"|"+result.getData());
//		}
	}


}
